package com.cdd.goods.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cdd.goods.mapper.GoodsBrandMapper;
import com.cdd.goods.mapper.GoodsMapper;
import com.cdd.goods.service.IGoodsBrandService;
import com.common.entity.GoodsBrand;
import com.common.entity.GoodsType;
import com.common.untils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lzl
 * @since 2022-09-01
 */
@Service
public class GoodsBrandServiceImpl implements IGoodsBrandService {
@Autowired
private GoodsBrandMapper goodsBrandMapper;
    @Override
    public CommonResult selectAll(Integer currentPage,Integer pageSize,GoodsBrand goodsBrand) {
        Page<GoodsBrand> page=new Page<>(currentPage,pageSize);
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("is_deleted",0);
        if (StringUtils.hasText(goodsBrand.getGbname())){
            wrapper.like("gbname",goodsBrand.getGbname());
        }
        if (goodsBrand.getIsDeleted()!=null){
            wrapper.eq("is_deleted",goodsBrand.getIsDeleted());
        }
        //List list = goodsBrandMapper.selectList(wrapper);
        Page page1 = goodsBrandMapper.selectPage(page, wrapper);
        return new CommonResult(2000,"查询成功",page1);
    }

    @Override
    public CommonResult delectGoodsBrand(Integer id) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("id",id);
        GoodsBrand goodsBrand=new GoodsBrand();
        goodsBrand.setIsDeleted(1);
        goodsBrandMapper.update(goodsBrand,wrapper);
        return new CommonResult(2000, "删除成功", null);
    }

    @Override
    public CommonResult updateGoodsBrand(GoodsBrand goodsBrand) {
        GoodsBrand goodsBrand1=new GoodsBrand();
        goodsBrand1.setId(goodsBrand.getId());
        goodsBrand1.setGbname(goodsBrand.getGbname());
        int i = goodsBrandMapper.updateById(goodsBrand1);
        System.out.println(i+"+++++++++++++++++++++++++");

        return new CommonResult(2000, "修改成功", null);
    }

    @Override
    public CommonResult insertGoods(GoodsBrand goodsBrand) {
        goodsBrand.setIsDeleted(0);
        List<GoodsBrand> goodsBrands = goodsBrandMapper.selectGbname();
        for (GoodsBrand g:goodsBrands) {
           if (g.getGbname().equals(goodsBrand.getGbname())){
               return new CommonResult(5000, "添加失败，已有该品牌", null);
           }

        }
        goodsBrandMapper.insert(goodsBrand);
        return new CommonResult(2000, "添加成功", null);

    }

    @Override
    public CommonResult select() {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("is_deleted",0);
        List list = goodsBrandMapper.selectList(wrapper);
        return new CommonResult(2000, "查询成功", list);
    }


}
